package com.example.blogdemo1.service;

import com.example.blogdemo1.common.constants.Constants;
import com.example.blogdemo1.common.model.BlogInfo;
import com.example.blogdemo1.common.utils.JWTUtils;
import com.example.blogdemo1.common.utils.MarkdownToTextUtils;
import com.example.blogdemo1.mapper.BlogMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.swing.text.AbstractDocument;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.springframework.util.StringUtils.*;

/*
 * @author zzr
 * @date: 2025/10/05  19:43
 * @description:
 */


@Slf4j
@Service
public class BlogService {

    @Autowired
    private BlogMapper blogMapper;


    public List<BlogInfo> getList() {
        List<BlogInfo> blogList = blogMapper.selectAll();
        for (BlogInfo blog : blogList) {
            String content = blog.getContent();
            String summary;

            // 若 content 为 null/空字符串/纯空白 → 直接设 summary 为空
            if (org.apache.commons.lang3.StringUtils.isBlank(content)) {
                summary = ""; // 也可设为“暂无摘要”等提示文字
            } else {
                // content 有效 → 调用工具类生成摘要
                summary = MarkdownToTextUtils.getSummary(content, 200);
            }
            blog.setSummary(summary);
        }
        return blogList;
    }

    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        BlogInfo blogDetail = blogMapper.selectById(blogId);
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JWTUtils.getIdByToken(token);
        // 判断作者是否为登录用户
        if (userId != null && userId == blogDetail.getUserId()) {
            blogDetail.setLoginUser(true);
        } else {
            blogDetail.setLoginUser(false);
        }
        return blogDetail;
    }

    public boolean insertBook(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        // 参数校验
        if (!hasLength(blogInfo.getTitle()) || !hasLength(blogInfo.getContent())) {
            return false;
        }
        // 获取登录用户
        // 获取 token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        // 从 token 中获取登录用户id
        Integer userId = JWTUtils.getIdByToken(token);
        if (userId == null || userId < 0) {
            return false;
        }
        blogInfo.setUserId(userId);

        try {
            Integer result = blogMapper.insertBlog(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (Exception e) {
            log.error("发布博客失败，e：", e);
        }
        return false;
    }


    public Boolean update(BlogInfo blogInfo) {

        if (blogInfo.getId() == null
            || !StringUtils.hasLength(blogInfo.getTitle())
            || !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        blogMapper.updateBlog(blogInfo);
        return true;
    }

    public Boolean delete(Integer blogId) {
        if (blogId <= 0) {
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1); // 删除
        blogMapper.updateBlog(blogInfo);
        return true;
    }
}
